<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=0.7, minimum-scale=0.2, maximum-scale=1.0, user-scalable=yes"/>
    <title>导航</title>
    <script src="common.js"></script>
    <link href="boot.css" rel="stylesheet">
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f4;
        }

        .container {
            text-align: center;
        }

        h2 {
            margin-bottom: 30px;
        }

        button {
            padding: 10px 20px;
            margin: 0 10px;
            font-size: 16px;
            border: none;
            border-radius: 5px;
            background-color: #007bff;
            color: #fff;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>请选择您要进行的操作：</h2>
    <button id="startOrStop" onclick="startOrStop()"></button>
    <button onclick="userManager()">战队管理</button>
    <button onclick="teamManager()">球队管理</button>
    <button onclick="viewSchedule()">赛程管理</button>
    <button onclick="logout()">退出账号</button>
</div>

<script>
    if (sessionStorage.getItem("username") === null || sessionStorage.getItem("username") === "undefined") {
        window.location.href = '/bp/login.html'; // 替换为你想要跳转的页面的URL
    }

    function viewSchedule() {
        window.location.href = '/bp/schedule.html'; // 替换为你想要跳转的页面的URL
    }

    function userManager() {
        window.location.href = '/bp/userManager.html'; // 替换为你想要跳转的页面的URL
    }

    function teamManager() {
        window.location.href = '/bp/teamManager.html'; // 替换为你想要跳转的页面的URL
    }

    function startOrStop() {
        fetch('http://' + ip + ':8080/startOrStop', {
            method: 'GET'
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                return response.json();
            })
            .then(data => {
                controller(true)
            });
    }

    function controller(b) {
        var startOrStop = document.getElementById('startOrStop');
        // 发送POST请求
        fetch('http://' + ip + ':8080/info', {
            method: 'GET'
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                return response.json();
            })
            .then(data => {
                if (data.result === true) {
                    startOrStop.textContent = "结束BP"
                    if (b) {
                        showAlert('开始完成', 'alert-success');
                    }
                } else {
                    startOrStop.textContent = "开始BP"
                    if (b) {
                        showAlert('结束完成', 'alert-success');
                    }
                }
            });
    }

    controller(false)
    setInterval(controller, 3000, false);

    function logout() {
        sessionStorage.removeItem("username")
        // 点击“退出账号”按钮后的操作，例如跳转到退出登录页面
        window.location.href = '/bp/login.html'; // 替换为实际的退出登录页面URL
    }

    function showAlert(message, className) {
        const alertDiv = document.createElement('div');
        alertDiv.className = `alert ${className} mt-4`;
        alertDiv.textContent = message;
        // 居中显示
        alertDiv.style.position = 'fixed';
        alertDiv.style.top = '50%';
        alertDiv.style.left = '50%';
        alertDiv.style.transform = 'translate(-50%, -50%)';

        document.body.appendChild(alertDiv);


        // 3秒后自动关闭弹窗
        setTimeout(() => {
            alertDiv.remove();
        }, 3000);
    }
</script>
</body>
</html>
